import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
import IboxIndex from '../views/IboxIndex.vue';
import MyLogin from '../views/login/MyLogin.vue';

import MyIndex from '../views/index/MyIndex.vue';

import MyBanner from '../views/banner/MyBanner.vue';
import AddBanner from '../views/banner/AddBanner.vue';
import BannerList from '../views/banner/BannerList.vue';

import MyProduct from '../views/product/MyProduct.vue';
import AddProduct from '../views/product/AddProduct.vue';
import ProductList from '../views/product/ProductList.vue';

import MyUser from '../views/user/MyUser.vue';
import AdminList from '../views/user/AdminList.vue';
import UserList from '../views/user/UserList.vue';

import MySet from '../views/set/MySet.vue';
import { ElMessage } from 'element-plus';


const routes: Array<RouteRecordRaw> = [
  // 路由重定向
  {
    path: '/',
    redirect: '/login'
  },
  // 登录路由
  {
    path:'/login',
    name:'login',
    component:MyLogin
  },
  // 首页路由
  {
    path: '/home',
    name: 'home',
    component: IboxIndex,
    // 二级路由
    children: [
      // 首页路由
      {
        path: '/index',
        name: 'index',
        component: MyIndex,
        meta: {
          title: "首页"
        }
      },
      // 轮播图路由
      {
        path: '/banner',
        name: 'banner',
        component: MyBanner,
        meta: {
          title: "轮播图管理"
        },
        children: [
          {
            path: '/addbanner',
            name: 'addbanner',
            component: AddBanner,
            meta: {
              classifyTitle: "轮播图管理",
              title: "添加轮播图"
            }
          },
          {
            path: '/bannerlist',
            name: 'bannerlist',
            component: BannerList,
            meta: {
              classifyTitle: "轮播图管理",
              title: "轮播图列表"
            }
          },
        ]
      },
      // 商品路由
      {
        path: '/product',
        name: 'product',
        component: MyProduct,
        meta: {
          title: "商品管理"
        },
        children: [
          {
            path: '/addproduct',
            name: 'addproduct',
            component: AddProduct,
            meta: {
              classifyTitle: "商品管理",
              title: "添加商品"
            }
          },
          {
            path: '/productlist',
            name: 'productlist',
            component: ProductList,
            meta: {
              classifyTitle: "商品管理",
              title: "商品列表"
            }
          },
        ]
      },
      // 账户路由
      {
        path: '/user',
        name: 'user',
        component: MyUser,
        meta: {
          title: "账户管理"
        },
        children: [
          {
            path: '/adminlist',
            name: 'adminlist',
            component: AdminList,
            meta: {
              classifyTitle: "账户管理",
              title: "管理员列表"
            }
          },
          {
            path: '/userlist',
            name: 'userlist',
            component: UserList,
            meta: {
              classifyTitle: "账户管理",
              title: "用户列表"
            }
          },
        ]
      },
      // 设置路由
      {
        path: '/setting',
        name: 'setting',
        component: MySet,
        meta: {
          title: "设置"
        }
      },
    ]
  }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})



// 使用路由导航守卫，全局使用，鉴权
router.beforeEach((to, from, next) => {
  // to 将要访问的路径
  // next 放行
  // 访问登录页面 直接放行
  // console.log('router-index.ts-路由导航守卫', to);
  // 如是登录页直接放行
  if (to.path === '/login') {
    return next()
  }
  // 读取本地token 做判断
  const tokenStr = window.localStorage.getItem('_token')
  // console.log('tokenStr', tokenStr);
  if (!tokenStr) {
    ElMessage({
      type: 'warning',
      message: '请先登录！'
    })
    return next('/login')
  }
  // 都通过，直接放行进入首页
  next()
})


export default router
